package com.xinghen.common.util;

import com.google.common.base.Preconditions;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;

/**
 * @author xinghen
 * @ClassName: RedisKeyUtil
 * @Package: com.xinghen.common.core.utils
 * @Description redis key 相关工具
 * @create 2018-04-16 15:30
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class RedisKeyUtil {

    private static final String RESET_PWD_TOKEN_KEY = "xinghen:cloud:restPwd";
    private static final String ACTIVE_USER = "xinghen:cloud:activeUser";
    private static final String SEND_SMS_COUNT = "xinghen:cloud:sms:count";
    private static final String SEND_EMAIL_CODE = "xinghen:cloud:email:code";
    private static final String ACCESS_TOKEN = "xinghen:cloud:token:accessToken";
    private static final String UPLOAD_FILE_SIZE = "xinghen:cloud:file:upload_file_size";
    private static final int REF_NO_MAX_LENGTH = 100;

    /**
     * 获取重置密码token的key
     *
     * @param resetPwdKey
     * @return
     */
    public static String getResetPwdTokenKey(String resetPwdKey) {
        Preconditions.checkArgument(StringUtils.isNotEmpty(resetPwdKey), "参数不能为空");
        return RESET_PWD_TOKEN_KEY + ":" + resetPwdKey;
    }

    /**
     * 获取发送邮件验证码的key
     *
     * @param loginName
     * @param email
     * @return
     */
    public static String getSendEmailCodeKey(String loginName, String email) {
        Preconditions.checkArgument(StringUtils.isNotEmpty(loginName), "Email不能为空");
        Preconditions.checkArgument(StringUtils.isNotEmpty(email), "用户名不能为空");
        return SEND_EMAIL_CODE + ":" + loginName + email;
    }

    /**
     * 获取激活用户的key
     *
     * @param activeToken
     * @return
     */
    public static String getActiveUserKey(String activeToken) {
        Preconditions.checkArgument(StringUtils.isNotEmpty(activeToken), "参数不能为空");
        return ACTIVE_USER + ":" + activeToken;
    }

    /**
     * 获取短信发送上限数的key
     *
     * @param ipAddr
     * @param type
     * @return
     */
    public static String getSendSmsCountKey(String ipAddr, String type) {
        Preconditions.checkArgument(StringUtils.isNotEmpty(ipAddr), "请不要篡改IP地址");
        return SEND_SMS_COUNT + ":" + type + ":" + ipAddr;
    }

    /**
     * 获取发送短信rate的key
     *
     * @param ipAddr
     * @return
     */
    public static String getSendSmsRateKey(String ipAddr) {
        Preconditions.checkArgument(StringUtils.isNotEmpty(ipAddr), "请不要篡改IP地址");
        return SEND_SMS_COUNT + ":" + ipAddr;
    }

    /**
     * 获取访问token的key
     *
     * @param token
     * @return
     */
    public static String getAccessTokenKey(String token) {
        Preconditions.checkArgument(StringUtils.isNotEmpty(token), "非法请求token参数不存在");
        return ACCESS_TOKEN + ":" + token;
    }

    /**
     * 获取文件限制的key
     *
     * @return
     */
    public static String getFileSizeKey() {
        return UPLOAD_FILE_SIZE;
    }

}
